using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.TurnBase
{
    public class UnitTouch : MonoBehaviour
    {
        private BattleUnit unit;

        public void Init(BattleUnit unit)
        {
            this.unit = unit;
        }

        public BattleUnit GetUnit()
        {
            return unit;
        }
    }
    public class ControlComponent : IComponent, ITransmit
    {
        //操作相关控件
        public static float gravity = 10f;

        private UnitTouch unitTouch;
        private BoxCollider collider;

        private GameObject pointerEffect;

        void IClass.OnReset()
        {

        }

        private BattleUnit unit;

        void IComponent.OnInitialize(BattleUnit unit)
        {
            this.unit = unit;
        }

        void IComponent.OnUpdate()
        {

        }

        void IComponent.OnBirth()
        {
            collider = unit.gameObject.AddComponent<BoxCollider>();
            collider.size = unit.Data.colliderSize;
            collider.center = unit.Data.colliderCenter;


            unitTouch = unit.gameObject.AddComponent<UnitTouch>();
            unitTouch.Init(unit);
        }

        void IComponent.OnDead()
        {

        }

        void ITransmit.OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (type == ETransmitType.OnSelected)
            {
                if (pointerEffect == null && !unit.Data.isDead)
                {
                    PveEffectManager.Instance.PlayEffectByType(PveResourceManager.EPveResType.Pointer, 3,
                        unit.Data.position, unit.Data.forward, 2, 0);
                    pointerEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, 2);
                    pointerEffect.transform.SetParent(unit.gameObject.transform);
                    pointerEffect.SetActive(true);
                    pointerEffect.transform.localPosition = Vector3.zero;
                }
            }
            else if (type == ETransmitType.OnUnSelected)
            {
                if (pointerEffect != null)
                {
                    PveResourceManager.RecoverResource(pointerEffect, PveResourceManager.EPveResType.Pointer, 1);
                    pointerEffect = null;
                }
            }
        }
    }
}